#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "hash.h"

/* alternierende quadratische sondierung*/
int qu_sond_next(int hash, int i)
{
	printf("conflicted! \n"); 
	return ((int)((hash + (pow(-1.0, (double)i) * pow(((double)i/2.0), 2.0)))) % HASHM) ;
}

/* Fowler-Noll-Vo hash function */
int fnv_hash(char* value)
{
	int i;
	unsigned long hash_ = FNV_BASE;
	
	for (i=0; i<strlen(value); i++) 
	{
		hash_ = hash_ ^ value[i];
		hash_ = hash_ * FNV_PRIME;
	}
	return (int)(((unsigned long)hash_) % HASHM);
}

int hash(char* value) 
{
	return fnv_hash(value);
}

void print_table(char** table)
{
	int i;
	for (i=0; i<HASHM; i++) 
	{
		printf("%d = %s\n", i, table[i]);
	}
}

void clear_table(char** table)
{
	int i;
	for (i=0; i<HASHM; i++) 
	{
		table[i] = NULL;
	}
}

int main(int argc, char** argv) 
{
	int i,j, h;
	char* val; 
	char** table;

	table = (char**)malloc(HASHM * sizeof(char*));
	
	clear_table(table);
	
	for (j=1; j<argc; j++) 
	{
		val = argv[j]; 
		h = hash(val);
		i = 1;
		printf("%s = %d\n", val, h);
		while (table[h] != NULL) 
		{
			h = qu_sond_next(h, i);
			printf("%s = %d\n", val, h);
			i++;
		}	 
		table[h] = val;
	}

	print_table(table);

}
